package com.codeanalysissys.backend.ruby;
import java.util.ArrayList;
import java.util.Objects;
public class RubyClass {
    public final String name;
    public final String filePath;
    public final String exTents;//继承
    private ArrayList<String> implementations = new ArrayList<>();
    ;//接口？
    //    public final String[] ImPorts;//
    private String typeParameters;//参数
    private ArrayList<RubyField> fields;//数据域
    private ArrayList<Rubymethod> methods;

    public RubyClass(String name, String filePath, String exTents) {
//        this.ImPorts = imports;
        this.name = name;
        this.filePath = filePath;
        this.exTents = exTents;
        fields = new ArrayList<>();
        methods = new ArrayList<>();
    }

    public void setTypeParameters(String params) {
        this.typeParameters = params;
    }

    public String getTypeParameters() {
        return this.typeParameters;
    }

    public void addField(RubyField field) {
        fields.add(field);
    }

    public boolean hasField(String name) {
        for (RubyField fld : fields) {
            if (fld.NAME.equals(name)) {
                return true;
            }
        }
        return false;
    }

    public RubyField[] getAllFields() {
        return fields.toArray(new RubyField[fields.size()]);
    }

    public void addMethod(Rubymethod method) {
        methods.add(method);
    }

    public boolean hasMethod(String name) {
        for (Rubymethod mtd : methods) {
            if (mtd.NAME.equals(name)) {
                return true;
            }
        }
        return false;
    }

    public String[] getimplements() {
        return implementations.toArray(new String[implementations.size()]);
    }

    public void addimplements(String imp) {
        implementations.add(imp);
    }

    public Rubymethod[] getAllMethods() {
        return methods.toArray(new Rubymethod[methods.size()]);
    }

    @Override
    public String toString() {
        String extend = exTents == null ? "null" : exTents;
        StringBuilder str = new StringBuilder("CLASS \n{\n");
        str.append("  NAME : \"").append(name).append("\",\n");
        str.append("  FILE : \"").append(filePath).append("\",\n");
        str.append("  FIELDS : \n  [\n");
        for (RubyField fld : fields) {
            str.append("    ").append(fld).append(",\n");
        }
        str.append("  ],\n");
        str.append("  METHODS : \n  [\n");
        for (Rubymethod mtd : methods) {
            str.append("    ").append(mtd).append(",\n");
        }
        str.append("  ]\n");
        str.append("}");
        return str.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof RubyClass)) {
            return false;
        }
        RubyClass cls = (RubyClass) obj;
        return this.name.equals(cls.name);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(name);
    }
}
